﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.HVAC;
using YArchitech.LIB;
using YArchitech.ProgressBar;
using YArchitech.Revit;
using Transfer.Utils;

namespace YArchitech.Plumbing
{
	public class MarkRiserPipeOperation
	{
		public MarkRiserPipeOperation(UIDocument uiDoc, FrmMarkRiserpipe form)
		{
			this.UIDoc = uiDoc;
			this.MarkForm = form;
		}

		public bool Execute()
		{
			bool flag = false;
			Transaction transaction = null;
			for (;;)
			{
				RevitVersionFuncs.ClearSelection(this.UIDoc.Selection);
				transaction = new Transaction(this.UIDoc.Document, "立管标注");
				transaction.Start();
				try
				{
					MarkType markingType = this.MarkForm.MarkingType;
					if (markingType != MarkType.singlePipeMarking)
					{
						if (markingType == MarkType.multiPipeMarking)
						{
							flag = this.MarkMultiPipes();
						}
					}
					else
					{
						flag = this.MarkSinglePipe();
					}
					if (flag)
					{
						transaction.Commit();
					}
					else
					{
						transaction.RollBack();
					}
					continue;
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
				{
					ex.ToString();
					if (transaction != null)
					{
						TransactionStatus status = transaction.GetStatus();
						if ((int)status == 1)
						{
							transaction.RollBack();
						}
						else if ((int)status == 5)
						{
							transaction.RollBack();
						}
					}
					if (this.MarkForm.isPicking)
					{
						this.MarkForm.isPicking = false;
						continue;
					}
				}
				catch (Exception ex2)
				{
					ex2.ToString();
					if (transaction != null)
					{
						TransactionStatus status2 = transaction.GetStatus();
						if ((int)status2 == 1)
						{
							transaction.RollBack();
						}
						else if ((int)status2 == 5)
						{
							transaction.RollBack();
						}
					}
					continue;
				}
				break;
			}
			this.MarkForm.Close();
			return flag;
		}

		protected bool MarkSinglePipe()
		{
			bool result = false;
			YJKWindowHandle owner = null;
			RiserPipeAndCenter riserPipeAndCenter = null;
			try
			{
				owner = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
				List<Autodesk.Revit.DB.View> sameTypeViews = this.GetSameTypeViews();
				if (this.SelectSinglePipe(this.UIDoc, ref riserPipeAndCenter))
				{
					this.DeleteRiserPipeMark(riserPipeAndCenter.RiserPipe);
					this.UIDoc.Document.Regenerate();
					using (SingleRiserPipeLinesJig singleRiserPipeLinesJig = new SingleRiserPipeLinesJig(this.UIDoc, this.MarkForm))
					{
						Parameter parameter = riserPipeAndCenter.RiserPipe.GetParameter("立管编号");
						if (parameter != null)
						{
							parameter.AsString();
						}
						singleRiserPipeLinesJig.StartPoint = riserPipeAndCenter.Location;
						XYZ xyz = this.UIDoc.Selection.PickPoint("请选择标注位置：");
						double angle = Convert.ToDouble(this.MarkForm.textLineAngle.Text);
						bool @checked = this.MarkForm.checkLineAngle.Checked;
						double distance = Convert.ToDouble(this.MarkForm.textDisToPipe.Text);
						if (this.MarkForm.checkLineAngle.Checked && this.MarkForm.textLineAngle.Text.Trim() == "")
						{
							YJKMessageBox.Information("引线角度不能为空!");
							return false;
						}
						if (!this.MarkForm.checkDisToPipe.Checked)
						{
							distance = AssistFunc.feetToMM(Math.Abs(xyz.Y - riserPipeAndCenter.Location.Y));
						}
						string str = string.Empty;
						string str2 = string.Empty;
						if (this.MarkForm.checkAreaNumber.Checked)
						{
							str = this.MarkForm.textAreaNumber.Text + "-";
						}
						if (this.MarkForm.checkFloorNumber.Checked)
						{
							str2 = this.MarkForm.textFloorNumber.Text + "-";
						}
						string areaAndFloor = str + str2;
						string text = this.MarkForm.textCustom.Text;
						if (this.MarkForm.checkBox1.Checked)
						{
							Parameter parameter2 = riserPipeAndCenter.RiserPipe.GetParameter("区号楼号");
							if (parameter2 != null)
							{
								areaAndFloor = parameter2.AsString();
							}
							else
							{
								areaAndFloor = "";
							}
						}
						MarkRiserPipeUtility.SetRiserPipeParameters(this.UIApp, riserPipeAndCenter.RiserPipe, areaAndFloor, text, this.MarkForm.radioCustom.Checked, this.MarkForm.checkBox1.Checked);
						double textLength = UnifiedModified.GetTextLength(this.MarkForm.checkAreaNumber.Checked, this.MarkForm.checkFloorNumber.Checked);
						string markFamilyPath = UnifiedModified.GetMarkFamilyPath(this.UIDoc.Document, "HY_水管立管标记");
						this.UIDoc.Document.LoadFamily(markFamilyPath);
						FamilySymbol familySymbol = YJKRevitTools.GetFamilySymbol(this.UIDoc.Document, "HY_水管立管标记", BuiltInCategory.OST_PipeTags);
						IndependentTag independentTag = RevitVersionFuncs.NewTag(this.UIDoc.Document, this.UIDoc.Document.ActiveView, new Reference(riserPipeAndCenter.RiserPipe), false, 0, 0, new XYZ(0.0, 0.0, 0.0));
						independentTag.ChangeTypeId(familySymbol.Id);
						MarkOperation markOperation = new MarkOperation();
						double num = 0.0;
						double num2 = 0.0;
						markOperation.GetTagTextWidthAndHeight(independentTag.TagText, this.UIDoc.Document, ref num, ref num2);
						textLength = markOperation.GetCreatTagLength(independentTag.TagText, this.UIDoc.Document);
						if (RevitVersion.GetVersionNumber(this.UIDoc.Document) == "2015" || RevitVersion.GetVersionNumber(this.UIDoc.Document) == "2014")
						{
							CommonFont fontSetting = new UserSetting().GetFontSetting();
							textLength = (double)independentTag.TagText.Length * num2 * fontSetting.FontGWRotia * 0.36;
						}
						num2 = markOperation.GetDistanceTagToLeader(this.UIDoc.Document, independentTag.TagText) * 304.8;
						this.UIDoc.Document.DeleteElement(independentTag);
						XYZ textLocation = null;
						XYZ subVector = null;
						List<Line> list = MarkRiserPipeUtility.DrawSinglePipeMarkLines(riserPipeAndCenter.Location, xyz, angle, @checked, distance, textLength, ref textLocation, ref subVector, this.UIDoc.Document, singleRiserPipeLinesJig.isLeft);
						DetailCurveArray detailCurveArray = new DetailCurveArray();
						foreach (Curve curve in list)
						{
							DetailCurve detailCurve = this.UIDoc.Document.Create.NewDetailCurve(this.UIDoc.ActiveView, curve);
							detailCurveArray.Append(detailCurve);
						}
						List<RiserPipeAndCenter> list2 = new List<RiserPipeAndCenter>();
						list2.Add(riserPipeAndCenter);
						Autodesk.Revit.DB.Group group = MarkRiserPipeUtility.WriteMarkGroupsToRiserPipe(this.UIDoc.Document, list2, detailCurveArray);
						AssociationMarkInfo.WriteMarkInfoToElement(group, YArchitech.HVAC.MarkType.RiserPipe);
						IndependentTag tag = null;
						MarkRiserPipeUtility.MarkSinglePipeText(this.UIDoc.Document, this.UIDoc.Document.ActiveView, riserPipeAndCenter.RiserPipe, textLocation, subVector, textLength, this.MarkForm.checkAreaNumber.Checked, this.MarkForm.checkFloorNumber.Checked, group, num2, ref tag);
						markOperation.SetAlignStyle(tag, new YJKLine(list.Last<Line>()), riserPipeAndCenter.Location, "水管");
						if (this.MarkForm.rboCurrentSystem.Checked)
						{
							List<Pipe> list3 = new List<Pipe>();
							list3.Add(riserPipeAndCenter.RiserPipe);
							int num3 = sameTypeViews.Count + 1;
							ProgressBarForm progressBarForm = new ProgressBarForm(num3, ProgressBarStyle.Blocks, true);
							if (num3 > 0)
							{
								progressBarForm.Text = "立管标注";
								progressBarForm.ShowInTaskbar = false;
								progressBarForm.Show(owner);
								progressBarForm.RefreshProgressBar();
							}
							foreach (Autodesk.Revit.DB.View view in sameTypeViews)
							{
								if (progressBarForm.m_stopTag)
								{
									return false;
								}
								if (view.GenLevel != null)
								{
									XYZ location = new XYZ(xyz.X, xyz.Y, view.GenLevel.Elevation);
									List<RiserPipeAndCenter> riserPipePointsInSpecifiedView = this.GetRiserPipePointsInSpecifiedView(view, list3, location);
									if (riserPipePointsInSpecifiedView != null && riserPipePointsInSpecifiedView.Count > 0)
									{
										detailCurveArray.Clear();
										List<Line> list4 = MarkRiserPipeUtility.DrawSinglePipeMarkLines(riserPipePointsInSpecifiedView[0].Location, location, angle, @checked, distance, textLength, ref textLocation, ref subVector, this.UIDoc.Document, singleRiserPipeLinesJig.isLeft);
										foreach (Curve curve2 in list4)
										{
											DetailCurve detailCurve2 = this.UIDoc.Document.Create.NewDetailCurve(view, curve2);
											detailCurveArray.Append(detailCurve2);
										}
										group = MarkRiserPipeUtility.WriteMarkGroupsToRiserPipe(this.UIDoc.Document, riserPipePointsInSpecifiedView, detailCurveArray);
										AssociationMarkInfo.WriteMarkInfoToElement(group, YArchitech.HVAC.MarkType.RiserPipe);
										MarkRiserPipeUtility.MarkSinglePipeText(this.UIDoc.Document, view, riserPipePointsInSpecifiedView[0].RiserPipe, textLocation, subVector, textLength, this.MarkForm.checkAreaNumber.Checked, this.MarkForm.checkFloorNumber.Checked, group, num2, ref tag);
										markOperation.SetAlignStyle(tag, new YJKLine(list4.Last<Line>()), riserPipePointsInSpecifiedView[0].Location, "水管");
									}
								}
								if (progressBarForm.m_stopTag)
								{
									return false;
								}
								progressBarForm.RefreshProgressBar();
							}
							progressBarForm.Dispose();
						}
						this.UIDoc.Document.Regenerate();
						result = true;
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
			return result;
		}

		protected bool MarkMultiPipes()
		{
			bool result = false;
			YJKWindowHandle owner = null;
			List<RiserPipeAndCenter> list = null;
			try
			{
				owner = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
				if (this.SelectMultiPipes(this.UIDoc, ref list))
				{
					foreach (RiserPipeAndCenter riserPipeAndCenter in list)
					{
						this.DeleteRiserPipeMark(riserPipeAndCenter.RiserPipe);
					}
					this.UIDoc.Document.Regenerate();
					using (MultiRiserPipesLinesJig multiRiserPipesLinesJig = new MultiRiserPipesLinesJig(this.UIDoc, this.MarkForm))
					{
						multiRiserPipesLinesJig.RiserPipePoints = list;
						XYZ xyz = this.UIDoc.Selection.PickPoint("请选择标注位置：");
						if (this.MarkForm.checkLineAngle.Checked && this.MarkForm.textLineAngle.Text.Trim() == "")
						{
							YJKMessageBox.Information("引线角度不能为空!");
							return false;
						}
						double num = 0.0;
						XYZ markVector = null;
						List<RiserPipeAndCenter> list2 = MarkRiserPipeUtility.SortPoints(list, xyz);
						string str = string.Empty;
						string str2 = string.Empty;
						if (this.MarkForm.checkAreaNumber.Checked)
						{
							str = this.MarkForm.textAreaNumber.Text + "-";
						}
						if (this.MarkForm.checkFloorNumber.Checked)
						{
							str2 = this.MarkForm.textFloorNumber.Text + "-";
						}
						string areaAndFloor = str + str2;
						string text = this.MarkForm.textCustom.Text;
						foreach (RiserPipeAndCenter riserPipeAndCenter2 in list2)
						{
							MarkRiserPipeUtility.SetRiserPipeParameters(this.UIApp, riserPipeAndCenter2.RiserPipe, areaAndFloor, text, this.MarkForm.radioCustom.Checked, this.MarkForm.checkBox1.Checked);
						}
						double height = 0.0;
						double num2 = 0.0;
						if (list.Count > 0)
						{
							string markFamilyPath = UnifiedModified.GetMarkFamilyPath(this.UIDoc.Document, "HY_水管立管标记");
							this.UIDoc.Document.LoadFamily(markFamilyPath);
							FamilySymbol familySymbol = YJKRevitTools.GetFamilySymbol(this.UIDoc.Document, "HY_水管立管标记", BuiltInCategory.OST_PipeTags);
							using (List<RiserPipeAndCenter>.Enumerator enumerator = list.GetEnumerator())
							{
								while (enumerator.MoveNext())
								{
									RiserPipeAndCenter riserPipeAndCenter3 = enumerator.Current;
									MarkOperation markOperation = new MarkOperation();
									IndependentTag independentTag = RevitVersionFuncs.NewTag(this.UIDoc.Document, this.UIDoc.Document.ActiveView, new Reference(riserPipeAndCenter3.RiserPipe), false, 0, 0, new XYZ(0.0, 0.0, 0.0));
									double num3 = 0.0;
									markOperation.GetTagTextWidthAndHeight(independentTag.TagText, this.UIDoc.Document, ref num3, ref num2);
									height = num2 + markOperation.GetLeaderDistance(this.UIDoc.Document);
									independentTag.ChangeTypeId(familySymbol.Id);
									num3 = markOperation.GetCreatTagLength(independentTag.TagText, this.UIDoc.Document);
									if (num <= num3)
									{
										num = num3 + 250.0;
									}
									if (RevitVersion.GetVersionNumber(this.UIDoc.Document) == "2015" || RevitVersion.GetVersionNumber(this.UIDoc.Document) == "2014")
									{
										CommonFont fontSetting = new UserSetting().GetFontSetting();
										num = (double)independentTag.TagText.Length * num2 * fontSetting.FontGWRotia * 0.36;
									}
									num2 = markOperation.GetDistanceTagToLeader(this.UIDoc.Document, independentTag.TagText) * 304.8;
									this.UIDoc.Document.DeleteElement(independentTag);
								}
								goto IL_42F;
							}
						}
						num = UnifiedModified.GetTextLength(this.MarkForm.checkAreaNumber.Checked, this.MarkForm.checkFloorNumber.Checked) + 250.0;
						IL_42F:
						if (list.Count > 1)
						{
							markVector = list2[list2.Count - 1].Location - list2[0].Location;
						}
						List<RiserPipeAndCenter> textLocationList = new List<RiserPipeAndCenter>();
						XYZ subVector = null;
						XYZ intersection = new XYZ();
						List<Line> list3 = MarkRiserPipeUtility.DrawMultiPipesMarkLines(list2, xyz, markVector, num, ref textLocationList, ref subVector, ref intersection, height);
						DetailCurveArray detailCurveArray = new DetailCurveArray();
						foreach (Curve curve in list3)
						{
							DetailCurve detailCurve = this.UIDoc.Document.Create.NewDetailCurve(this.UIDoc.ActiveView, curve);
							detailCurveArray.Append(detailCurve);
						}
						Autodesk.Revit.DB.Group group = MarkRiserPipeUtility.WriteMarkGroupsToRiserPipe(this.UIDoc.Document, list, detailCurveArray);
						AssociationMarkInfo.WriteMarkInfoToElement(group, YArchitech.HVAC.MarkType.RiserPipe);
						this.MarkMultiPipesText(this.UIDoc.Document.ActiveView, textLocationList, markVector, subVector, num, this.MarkForm.checkAreaNumber.Checked, this.MarkForm.checkFloorNumber.Checked, group, num2, list3, intersection);
						if (this.MarkForm.rboCurrentSystem.Checked)
						{
							List<Autodesk.Revit.DB.View> sameTypeViews = this.GetSameTypeViews();
							int num4 = sameTypeViews.Count + 1;
							ProgressBarForm progressBarForm = new ProgressBarForm(num4, ProgressBarStyle.Blocks, true);
							if (num4 > 0)
							{
								progressBarForm.Text = "立管标注";
								progressBarForm.ShowInTaskbar = false;
								progressBarForm.Show(owner);
								progressBarForm.RefreshProgressBar();
							}
							List<Pipe> list4 = new List<Pipe>();
							foreach (RiserPipeAndCenter riserPipeAndCenter4 in list2)
							{
								list4.Add(riserPipeAndCenter4.RiserPipe);
							}
							foreach (Autodesk.Revit.DB.View view in sameTypeViews)
							{
								if (progressBarForm.m_stopTag)
								{
									return false;
								}
								if (view.GenLevel != null)
								{
									XYZ location = new XYZ(xyz.X, xyz.Y, view.GenLevel.Elevation);
									List<RiserPipeAndCenter> riserPipePointsInSpecifiedView = this.GetRiserPipePointsInSpecifiedView(view, list4, location);
									if (riserPipePointsInSpecifiedView != null)
									{
										detailCurveArray.Clear();
										XYZ intersection2 = new XYZ();
										List<Line> list5 = MarkRiserPipeUtility.DrawMultiPipesMarkLines(riserPipePointsInSpecifiedView, location, markVector, num, ref textLocationList, ref subVector, ref intersection2, height);
										foreach (Curve curve2 in list5)
										{
											DetailCurve detailCurve2 = this.UIDoc.Document.Create.NewDetailCurve(view, curve2);
											detailCurveArray.Append(detailCurve2);
										}
										group = MarkRiserPipeUtility.WriteMarkGroupsToRiserPipe(this.UIDoc.Document, riserPipePointsInSpecifiedView, detailCurveArray);
										AssociationMarkInfo.WriteMarkInfoToElement(group, YArchitech.HVAC.MarkType.RiserPipe);
										this.MarkMultiPipesText(view, textLocationList, markVector, subVector, num, this.MarkForm.checkAreaNumber.Checked, this.MarkForm.checkFloorNumber.Checked, group, num2, list5, intersection2);
									}
								}
								if (progressBarForm.m_stopTag)
								{
									return false;
								}
								progressBarForm.RefreshProgressBar();
							}
							progressBarForm.Dispose();
						}
						this.UIDoc.Document.Regenerate();
						result = true;
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
			return result;
		}

		protected bool SelectSinglePipe(UIDocument uiDoc, ref RiserPipeAndCenter pipeAndCenter)
		{
			bool result = false;
			if (uiDoc != null && uiDoc.Selection != null)
			{
				Selection selection = uiDoc.Selection;
				RevitVersionFuncs.ClearSelection(selection);
				Reference reference = selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new PipeFilter(), "请选择要标注的立管:");
				if (reference != null)
				{
					if (!this.MarkForm.CheckBeforeDraw())
					{
						return false;
					}
					Pipe pipe = uiDoc.Document.GetElement(reference) as Pipe;
					if (YJKRevitTools.isRiserPipe(pipe))
					{
						result = true;
						XYZ riserPipeCenter = this.GetRiserPipeCenter(pipe);
						pipeAndCenter = new RiserPipeAndCenter(pipe, riserPipeCenter);
					}
					else
					{
						result = false;
						pipeAndCenter = null;
					}
				}
			}
			return result;
		}

		protected bool SelectMultiPipes(UIDocument uiDoc, ref List<RiserPipeAndCenter> pipeAndCenterList)
		{
			bool result = false;
			pipeAndCenterList = new List<RiserPipeAndCenter>();
			if (uiDoc != null && uiDoc.Selection != null)
			{
				Selection selection = uiDoc.Selection;
				RevitVersionFuncs.ClearSelection(selection);
				IList<Element> list = selection.PickElementsByRectangle(new PipeFilter(), "请选择要标注的立管（共线）:");
				if (list.Count > 1)
				{
					if (!this.MarkForm.CheckBeforeDraw())
					{
						return false;
					}
					foreach (Element element in list)
					{
						Pipe pipe = (Pipe)element;
						if (YJKRevitTools.isRiserPipe(pipe))
						{
							Pipe pipe2 = pipe;
							XYZ point = this.GetRiserPipeCenter(pipe2);
							if (pipeAndCenterList == null || !(from s in pipeAndCenterList
							where s.Location.IsAlmostEqualTo(point)
							select s).Any<RiserPipeAndCenter>())
							{
								RiserPipeAndCenter item = new RiserPipeAndCenter(pipe2, point);
								pipeAndCenterList.Add(item);
							}
						}
					}
					if (this.PipesIsRiserPipe(pipeAndCenterList))
					{
						if (this.PointsInOneLine(pipeAndCenterList))
						{
							result = true;
						}
						else
						{
							YJKMessageBox.Information("非共线立管无法创建多管标注！");
							result = false;
							pipeAndCenterList.Clear();
						}
					}
					else
					{
						YJKMessageBox.Information("所选管道不全为立管！");
						result = false;
						pipeAndCenterList.Clear();
					}
				}
			}
			return result;
		}

		protected XYZ GetRiserPipeCenter(Pipe pipe)
		{
			Line curve = (pipe.Location as LocationCurve).Curve as Line;
			XYZ hyendPoint = curve.GetEndPoint(0);
			curve.GetEndPoint(1);
			double elevation = this.UIDoc.ActiveView.GenLevel.Elevation;
			return new XYZ(hyendPoint.X, hyendPoint.Y, elevation);
		}

		protected bool PipesIsRiserPipe(List<RiserPipeAndCenter> pipes)
		{
			bool flag = true;
			if (pipes.Count == 0)
			{
				flag = false;
			}
			foreach (RiserPipeAndCenter riserPipeAndCenter in pipes)
			{
				flag &= YJKRevitTools.isRiserPipe(riserPipeAndCenter.RiserPipe);
			}
			return flag;
		}

		protected bool PointsInOneLine(List<RiserPipeAndCenter> points)
		{
			bool flag = false;
			if (points.Count > 0 && points.Count < 3)
			{
				flag = true;
			}
			if (points.Count > 2)
			{
				flag = Geometry.Is_Points_Collinear(points[0].Location, points[1].Location, points[2].Location);
				if (flag)
				{
					YJKLine hyline = new YJKLine(points[0].Location, points[1].Location);
					for (int i = 3; i < points.Count; i++)
					{
						flag &= hyline.IsPointOnCurveLine(points[i].Location);
					}
				}
			}
			return flag;
		}

		protected List<Pipe> GetConnectedPipes(Pipe pipe)
		{
			List<Pipe> list = new List<Pipe>();
			if (pipe != null)
			{
				list.Add(pipe);
				foreach (object obj in pipe.ConnectorManager.Connectors)
				{
					Connector connector = (Connector)obj;
					SearchSystem searchSystem = new SearchSystem(this.UIDoc, SearchSystemFilter.ByType);
					searchSystem.Run(pipe, connector.Origin);
					foreach (SearchNode searchNode in searchSystem.Nodes)
					{
						Pipe pipe2 = searchNode.Element as Pipe;
						if (pipe2 != null && !list.Contains(pipe2) && YJKRevitTools.isRiserPipe(pipe2) && this.IsSamePlanimetricPosition(pipe, pipe2))
						{
							list.Add(pipe2);
						}
					}
				}
			}
			return list;
		}

		protected bool IsSamePlanimetricPosition(Pipe pipeFirst, Pipe pipeSecond)
		{
			bool result = false;
			if (YJKRevitTools.isRiserPipe(pipeFirst) && YJKRevitTools.isRiserPipe(pipeSecond))
			{
				Line curve = (pipeFirst.Location as LocationCurve).Curve as Line;
				XYZ hyendPoint = curve.GetEndPoint(0);
				curve.GetEndPoint(1);
				Line curve2 = (pipeSecond.Location as LocationCurve).Curve as Line;
				XYZ hyendPoint2 = curve2.GetEndPoint(0);
				XYZ hyendPoint3 = curve2.GetEndPoint(1);
				if (Math.Abs(hyendPoint2.X - hyendPoint.X) < 0.0001 && Math.Abs(hyendPoint2.Y - hyendPoint.Y) < 0.0001 && Math.Abs(hyendPoint3.X - hyendPoint.X) < 0.0001 && Math.Abs(hyendPoint3.Y - hyendPoint.Y) < 0.0001)
				{
					result = true;
				}
			}
			return result;
		}

		protected List<RiserPipeAndCenter> GetRiserPipePointsInSpecifiedView(Autodesk.Revit.DB.View view, List<Pipe> pipes, XYZ location)
		{
			List<RiserPipeAndCenter> list = new List<RiserPipeAndCenter>();
			double elevation = view.GenLevel.Elevation;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(view.Document, view.Id);
			List<Pipe> list2 = null;
			foreach (Pipe pipe in pipes)
			{
				using (List<Pipe>.Enumerator enumerator2 = this.GetConnectedPipes(pipe).GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						Pipe connectedPipe = enumerator2.Current;
						Line curve = (connectedPipe.Location as LocationCurve).Curve as Line;
						XYZ xyz = curve.GetEndPoint(0);
						XYZ xyz2 = curve.GetEndPoint(1);
						if (xyz.Z > xyz2.Z)
						{
							XYZ xyz3 = xyz;
							xyz = xyz2;
							xyz2 = xyz3;
						}
						if (Geometry.Lessthan_Or_Equal(xyz.Z, elevation) && Geometry.Greaterthan_Or_Equal(xyz2.Z, elevation))
						{
							this.setPipeParameter(xyz, elevation, connectedPipe, pipe, ref list);
						}
						else
						{
							if (list2 == null)
							{
								list2 = filteredElementCollector.OfClass(typeof(Pipe)).Cast<Pipe>().ToList<Pipe>();
							}
							if (list2 != null && (from s in list2
							where s.Id == connectedPipe.Id
							select s).Any<Pipe>())
							{
								this.setPipeParameter(xyz, elevation, connectedPipe, pipe, ref list);
							}
						}
					}
				}
			}
			int count = list.Count;
			return MarkRiserPipeUtility.SortPoints(list, location);
		}

		private void setPipeParameter(XYZ startPoint, double elevation, Pipe connectedPipe, Pipe pipe, ref List<RiserPipeAndCenter> resultList)
		{
			XYZ point = new XYZ(startPoint.X, startPoint.Y, elevation);
			RiserPipeAndCenter item = new RiserPipeAndCenter(connectedPipe, point);
			resultList.Add(item);
			Parameter parameter = pipe.GetParameter("立管编号");
			if (parameter != null && parameter.HasValue)
			{
				string text = parameter.AsString();
				parameter = connectedPipe.GetParameter("立管编号");
				if (parameter != null)
				{
					parameter.Set(text);
				}
			}
			Parameter parameter2 = pipe.GetParameter("区号楼号");
			if (parameter2 != null && parameter2.HasValue)
			{
				string text2 = parameter2.AsString();
				parameter2 = connectedPipe.GetParameter("区号楼号");
				if (parameter2 != null)
				{
					parameter2.Set(text2);
				}
			}
		}

		protected List<Autodesk.Revit.DB.View> GetSameTypeViews()
		{
			List<Autodesk.Revit.DB.View> list = new List<Autodesk.Revit.DB.View>();
			Autodesk.Revit.DB.View activeView = this.UIDoc.Document.ActiveView;
			if (activeView.GetParameter("视图分类-父") == null || activeView.GetParameter("视图分类-子") == null)
			{
				return list;
			}
			string b = activeView.GetParameter("视图分类-父").AsString();
			string b2 = activeView.GetParameter("视图分类-子").AsString();
			foreach (Element element in YJKElementFilter.GetElementByCategory(this.UIDoc.Document, BuiltInCategory.OST_Views))
			{
				ViewPlan viewPlan = ((Autodesk.Revit.DB.View)element) as ViewPlan;
				if (viewPlan != null && viewPlan.GetParameter("视图分类-父") != null && viewPlan.GetParameter("视图分类-子") != null && viewPlan != null && viewPlan.Id != activeView.Id && !viewPlan.IsTemplate && viewPlan.GetParameter("视图分类-父").AsString() == b && viewPlan.GetParameter("视图分类-子").AsString() == b2)
				{
					list.Add(viewPlan);
				}
			}
			return list;
		}

		protected bool MarkMultiPipesText(Autodesk.Revit.DB.View view, List<RiserPipeAndCenter> textLocationList, XYZ markVector, XYZ subVector, double textLength, bool withArea, bool withFloor, Autodesk.Revit.DB.Group group, double height, List<Line> listLine, XYZ intersection)
		{
			bool result = false;
			bool flag = false;
			XYZ xyz = new XYZ(0.0, 1.0, 0.0);
			XYZ xyz2 = new XYZ(1.0, 0.0, 0.0);
			new XYZ(-1.0, 0.0, 0.0);
			if (Geometry.IsEqual(markVector.Y, 0.0))
			{
				flag = true;
				using (List<RiserPipeAndCenter>.Enumerator enumerator = textLocationList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						RiserPipeAndCenter riserPipeAndCenter = enumerator.Current;
						if (Geometry.GreaterThan(subVector.Y, 0.0))
						{
							riserPipeAndCenter.Location = riserPipeAndCenter.Location.Add(xyz * AssistFunc.mmToFeet(textLength / 2.0));
						}
						else
						{
							riserPipeAndCenter.Location = riserPipeAndCenter.Location.Add(-xyz * AssistFunc.mmToFeet(textLength / 2.0));
						}
						if (Geometry.LessThan(markVector.X, 0.0))
						{
							riserPipeAndCenter.Location = riserPipeAndCenter.Location.Add(-xyz2 * AssistFunc.mmToFeet(height));
						}
						else
						{
							riserPipeAndCenter.Location = riserPipeAndCenter.Location.Add(-xyz2 * AssistFunc.mmToFeet(height));
						}
					}
					goto IL_238;
				}
			}
			flag = false;
			foreach (RiserPipeAndCenter riserPipeAndCenter2 in textLocationList)
			{
				riserPipeAndCenter2.Location = riserPipeAndCenter2.Location.Add(xyz2 * AssistFunc.mmToFeet(textLength / 2.0)).Add(xyz * AssistFunc.mmToFeet(height));
				if (Geometry.LessThan(subVector.X, 0.0))
				{
					riserPipeAndCenter2.Location = riserPipeAndCenter2.Location.Add(subVector.Normalize() * AssistFunc.mmToFeet(textLength));
				}
			}
			IL_238:
			string markFamilyPath = UnifiedModified.GetMarkFamilyPath(this.UIDoc.Document, "HY_水管立管标记");
			bool flag2 = this.UIDoc.Document.LoadFamily(markFamilyPath);
			if (!flag2)
			{
				FamilySymbol familySymbol = YJKRevitTools.GetFamilySymbol(this.UIDoc.Document, "HY_水管立管标记", BuiltInCategory.OST_PipeTags);
				if (familySymbol != null)
				{
					flag2 = true;
				}
			}
			TagOrientation tagOrientation = 0;
			if (flag)
			{
                tagOrientation = (TagOrientation)1;
			}
			if (flag2)
			{
				FamilySymbol familySymbol = YJKRevitTools.GetFamilySymbol(this.UIDoc.Document, "HY_水管立管标记", BuiltInCategory.OST_PipeTags);
				string text = string.Empty;
				MarkOperation markOperation = new MarkOperation();
				YJKLine hyLine = new YJKLine(listLine.Last<Line>());
				for (int i = 0; i < textLocationList.Count; i++)
				{
					RiserPipeAndCenter riserPipeAndCenter3 = textLocationList[i];
					IndependentTag independentTag = RevitVersionFuncs.NewTag(this.UIDoc.Document, view, new Reference(riserPipeAndCenter3.RiserPipe), false, 0, tagOrientation, riserPipeAndCenter3.Location);
					independentTag.ChangeTypeId(new ElementId(familySymbol.Id.IntegerValue));
					independentTag.HasLeader = false;
					if (!string.IsNullOrEmpty(text))
					{
						text += "*";
					}
					markOperation.SetAlignStyleRiserPipe(independentTag, hyLine, intersection, "水管");
					text += independentTag.Id.IntegerValue.ToString();
					AssociationMarkInfo.WriteMarkInfoToElement(independentTag, YArchitech.HVAC.MarkType.RiserPipe);
					MarkRiserPipeUtility.WritePipeTagInfo(riserPipeAndCenter3.RiserPipe, independentTag);
				}
				MarkRiserPipeUtility.WriteTagsInfo(group, text);
				result = true;
			}
			return result;
		}

		private List<Element> GetElementByCategory(Autodesk.Revit.DB.Document doc, BuiltInCategory builtInCategory)
		{
			return new FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(builtInCategory).ToElements().ToList<Element>();
		}

		protected bool DeleteRiserPipeMark(Pipe riserPipe)
		{
			bool result = false;
			if (riserPipe != null)
			{
				foreach (Pipe elem in this.GetConnectedPipes(riserPipe))
				{
					string text = MarkRiserPipeUtility.ReadDetailGroupsInfo(elem);
					List<Element> elementByCategory;
					if (this.MarkForm.rboCurrentLevel.Checked)
					{
						elementByCategory = this.GetElementByCategory(this.UIDoc.Document, BuiltInCategory.OST_IOSDetailGroups);
					}
					else
					{
						elementByCategory = YJKElementFilter.GetElementByCategory(this.UIDoc.Document, BuiltInCategory.OST_IOSDetailGroups);
					}
					MarkRiserPipeUtility.WritePipeTagInfo(elem, string.Empty);
					if (text != null)
					{
						List<string> list = text.Split(new char[]
						{
							'*'
						}).ToList<string>();
						foreach (Element element in elementByCategory)
						{
							if (list.Contains(element.Id.ToString()))
							{
								this.DeleteTagToggledInGroup(element);
								this.UIDoc.Document.DeleteElement(element);
							}
						}
						MarkRiserPipeUtility.WriteDetailGroupsInfo(elem, string.Empty);
						MarkRiserPipeUtility.WritePipeTagInfo(elem, string.Empty);
					}
				}
			}
			return result;
		}

		protected bool DeleteTagToggledInGroup(Element group)
		{
			bool result = false;
			string text = MarkRiserPipeUtility.ReadTagsInfo(group);
			ICollection<Element> collection = new FilteredElementCollector(this.UIDoc.Document).OfCategory(BuiltInCategory.OST_PipeTags).OfClass(typeof(IndependentTag)).ToList<Element>();
			if (text != null)
			{
				List<string> list = text.Split(new char[]
				{
					'*'
				}).ToList<string>();
				foreach (Element element in collection)
				{
					IndependentTag independentTag = element as IndependentTag;
					if (independentTag != null && list.Contains(independentTag.Id.ToString()))
					{
						this.UIDoc.Document.DeleteElement(independentTag);
					}
				}
			}
			return result;
		}

		public UIApplication UIApp;

		public UIDocument UIDoc;

		public FrmMarkRiserpipe MarkForm;
	}
}
